package newbie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleServlet extends HttpServlet{

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	public void init(ServletConfig conf) throws ServletException{
		super.init(conf);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse respones) throws ServletException, IOException{
		respones.setContentType("text/html");
		PrintWriter p = respones.getWriter();

		p.println("<html>");
		p.println("<title>");
		p.println("Simple servlet");
		p.println("</title>");

		p.println("<body>");

		p.println("<h3>");
		p.println("Simple servlet example");
		p.println("<h3>");

		p.println("<p>" + request.getProtocol().toString() + "</p>");
		p.println("<p>" + request.getRequestURI().toString() + "</p>");
		p.println("<p>" + request.getParameterValues("a")[0] + "</p>");

		Map m = request.getParameterMap();
		Set keySet = m.keySet();
		Iterator i = keySet.iterator();

		while(i.hasNext()) {
			Object key = (Object) i.next();
			Object val = (Object) m.get(key);
			p.println("<p>" + key.getClass() + " = " + val.getClass() + "</p>");
		}

	//	respones.setHeader("Refresh", "5"); // refresh sau 5s

		p.println((new Date()).toString());
		p.println("</body>");
		p.println("</html>");
	}


}
